import Vuex from 'vuex'
import Vue from 'vue'
import axios from 'axios'
import dbconf from '@/conf/db.conf'

Vue.use(Vuex)
Vue.prototype.ajax = axios
let Store = new Vuex.Store({
  strict: true,
  /**
   * @rigthPage 控制右侧组件,默认为main
   * @article   文章数组
   * @tips      标签数组
   * @articlebytips articlebytips 通过标签查出的文章数据
   * @cur_page  当前页
   * @tipCur_page 通过标签查出的文章当前页
   * @status    加载状态码
   * @tipStatus    标签加载状态码
   * @changeTip  旧标签标记
   * @changeTip  是否切换了标签
   * @loading    加载动画
   * @err        是否出错
   * @Redirect   是否跳转路由
   */
  state: {
    rigthPage: 'Tips-article-qzkf',
    article: [],
    tips: [],
    articlebytips: [],
    cur_page: 0,
    tipCur_page: 0,
    status: '加载更多',
    tipStatus: '加载更多',
    oldTips: '',
    changeTip: false,
    loading: false,
    err: 0,
    redirect: false
  },
  mutations: {
    // 加载更多时,增加当前页
    addPage (state) {
      state.cur_page++
    },
    // TIPS文章加载更多时,增加当前页
    addTipPage (state) {
      state.tipCur_page++
    },
    // 切换tips文章时,重置当前页
    reTippage (state) {
      state.tipCur_page = 0
    },
    // 设置右侧主页面组件
    setRigthPage (state, arg) {
      state.rigthPage = arg
    },
    // 查询全部文章
    queryArticleList (state, arg) {
      state.article = (arg === '') ? state.article : state.article.concat(arg)
      if (arg === '') state.status = '没有更多了'
    },
    // 查询全部tips
    getTips (state, arg) {
      state.tips = arg
    },
    // 监听是否改变了tips
    changeTipIng (state, arg) {
      state.changeTip = true
    },
    // 通过tips查询所有文章
    queryArticleListByTips (state, arg) {
      if (state.changeTip) {
        state.articlebytips = arg
        state.changeTip = false
        state.tipStatus = '加载更多'
        return
      }
      state.articlebytips = (arg === '') ? state.articlebytips : state.articlebytips.concat(arg)
      if (arg === '') state.tipStatus = '没有更多了'
    },
    // 切换tips时,保存之前的tips
    setOldTips (state, arg) {
      state.oldTips = arg
    },
    // 开启加载中
    startLoad (state) {
      state.loading = true
    },
    // 关闭加载中
    endLoad (state) {
      state.loading = false
    },
    // 设置状态
    setStatus (state, arg) {
      state.status = arg
    },
    setTipStatus (state, arg) {
      state.status = arg
    },
    setErr (state, arg) {
      state.err = arg
    },
    setRedirect (state, arg) {
      state.redirect = arg
    }
  },
  actions: {
    // 设置右侧主页面组件
    setRigthPage ({commit}, arg) {
      commit('setRigthPage', arg)
    },
    // 查询全部文章
    queryArticleList ({state, commit}, arg) {
      if (arg !== state.cur_page && arg !== undefined) {
        return
      }
      commit('startLoad')
      commit('setStatus', '加载中')
      axios.get(dbconf.url + 'article?page=' + state.cur_page).then(res => {
        // if (res.data.err === '1') {
        //   commit('setErr', 1)
        // }
        commit('queryArticleList', res.data.length === 0 ? '' : res.data)
        commit('addPage')
        commit('endLoad')
        if (state.status === '加载中') {
          commit('setStatus', '加载更多')
        }
      }, err => {
        console.log(err)
      })
    },
    // 查询全部tips
    getTips ({commit}, arg) {
      axios.get(dbconf.url + 'tips').then(res => {
        commit('getTips', res.data)
      }, err => {
        console.log(err)
      })
    },
    // 通过tips查询所有文章
    queryArticleListByTips ({state, commit}, arg) {
      if (arg[1] !== state.tipCur_page && arg[1] !== undefined) {
        return
      }
      if (arg.length === 3 && arg[2] === 'changetip') {
        commit('changeTipIng')
      }
      commit('startLoad')
      commit('setTipStatus', '加载中')
      commit('setStatus', '加载更多')
      axios.get(dbconf.url + 'queryArticleListByTips?tips=' + arg[0] + '&&page=' + state.tipCur_page).then(res => {
        commit('queryArticleListByTips', res.data.length === 0 ? '' : res.data)
        commit('addTipPage')
        commit('endLoad')
        if (state.tipStatus === '加载中') {
          commit('setTipStatus', '加载更多')
        }
      }, err => {
        console.log(err)
      })
    },
    // 切换tips时,保存之前的tips
    setOldTips ({state, commit}, arg) {
      commit('setOldTips', arg)
    },
    // 切换tips文章时,重置当前页
    reTippage ({state, commit}, arg) {
      commit('reTippage')
    },
    // 开启加载中
    startLoad ({commit}, arg) {
      commit('startLoad')
    },
    // 关闭加载中
    endLoad ({commit}, arg) {
      commit('endLoad')
    },
    setRedirect ({commit}, arg) {
      commit('setRedirect', arg)
    }
  },
  getters: {
    getTips (state) {
      if (state.tips.length === 0) {
        Store.dispatch('getTips')
      }
      return state.tips
    }
  }
})
export default Store
